home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac: Not for Sale / Another.not.for.sale (Australia).iso / fade into you / getting there / TCP⁄PPP⁄SLIP / TCP⁄IP Scripting Addition 1.1.2 / Examples 1.2 / Text Versions / Run and Get FTP 1.2 (text) < prev    next >
Text File  |  1994-09-26  |  7KB  |  208 lines

  1. (*
  2. Run and Get FTP
  3. version 1.2
  4. Copyright © 1994 by Atul Butte. All Rights Reserved.
  5.  
  6. Run me to get all the files in a specified directory on a Unix host (works best after using the Drag and Drop FTP sample).
  7.  
  8. Atul Butte
  9. atul_butte@brown.edu
  10. *)
  11.  
  12. global LF, CR, CRLF
  13.  
  14. property ftp_set : false
  15. property ftp_host : ""
  16. property ftp_user : ""
  17. property ftp_directory : ""
  18. property ftp_localname : ""
  19. property ftp_transfer_type : ""
  20.  
  21. on run
  22.     set dialog_response to (display dialog "Enter the name of the machine where you want to get files" default answer ftp_host)
  23.     if (button returned of dialog_response ≠ "OK") then
  24.         return
  25.     else
  26.         set ftp_host to text returned of dialog_response
  27.     end if
  28.     
  29.     set dialog_response to (display dialog "Enter your user–id on " & ftp_host default answer ftp_user)
  30.     if (button returned of dialog_response ≠ "OK") then
  31.         return
  32.     else
  33.         set ftp_user to text returned of dialog_response
  34.     end if
  35.     
  36.     set dialog_response to (display dialog "Enter the directory of files to get on " & ftp_host & " to get" default answer ftp_directory)
  37.     if (button returned of dialog_response ≠ "OK") then
  38.         return
  39.     else
  40.         set ftp_directory to text returned of dialog_response
  41.     end if
  42.     
  43.     set dialog_response to (display dialog "Do you want these downloaded as (1) MacBinary files, (2) text files, or (3) raw data files?" default answer "1")
  44.     if (button returned of dialog_response ≠ "OK") then
  45.         return
  46.     else
  47.         set ftp_transfer_type to ((text returned of dialog_response) as integer)
  48.     end if
  49.     
  50.     set ftp_localname to (choose folder "Select destination for files") as string
  51.     
  52.     set LF to (ASCII character of 10)
  53.     set CR to (ASCII character of 13)
  54.     set CRLF to CR & LF
  55.     set oldDelimiters to AppleScript's text item delimiters
  56.     set AppleScript's text item delimiters to {"."}
  57.     set localAddr to (tcp my address)
  58.     set localAddrList to text items of localAddr
  59.     set AppleScript's text item delimiters to {","}
  60.     set localAddrCommaString to localAddrList as string
  61.     set AppleScript's text item delimiters to oldDelimiters
  62.     
  63.     set dialog_response to (display dialog "Enter the password for " & ftp_user & "@" & ftp_host & return & "Note: what you type is visible!" default answer "")
  64.     if (button returned of dialog_response ≠ "OK") then
  65.         return
  66.     else
  67.         set ftp_password to text returned of dialog_response
  68.     end if
  69.     
  70.     set sss to (tcp connect to host ftp_host port 21)
  71.     try
  72.         readresponse(sss)
  73.         if (ftp_user ≠ "") then
  74.             tcp write stream sss data "USER " & ftp_user & return using ISO88591
  75.             readresponse(sss)
  76.         end if
  77.         if (ftp_password ≠ "") then
  78.             tcp write stream sss data "PASS " & ftp_password & return using ISO88591
  79.             readresponse(sss)
  80.         end if
  81.         if (ftp_directory ≠ "") then
  82.             tcp write stream sss data "CWD " & ftp_directory & return using ISO88591
  83.             readresponse(sss)
  84.         end if
  85.         
  86.         set nlst_stream to (tcp wait for connect)
  87.         
  88.         set nlst_stream_status to (tcp status stream nlst_stream)
  89.         set nlstHiByte to round (local port of nlst_stream_status) / 256 rounding down
  90.         set nlstLoByte to (local port of nlst_stream_status) mod 256
  91.         
  92.         tcp write stream sss data "PORT " & localAddrCommaString & "," & nlstHiByte & "," & nlstLoByte & return using ISO88591
  93.         readresponse(sss)
  94.         
  95.         tcp write stream sss data "TYPE A" & return using ISO88591
  96.         readresponse(sss)
  97.         
  98.         tcp write stream sss data "NLST" & return using ISO88591
  99.         readresponse(sss)
  100.         
  101.         repeat until (connection status of (tcp status stream nlst_stream) = Connected)
  102.         end repeat
  103.         
  104.         set listOfFilesOnHost to ""
  105.         repeat until (connection status of (tcp status stream nlst_stream) ≠ Connected)
  106.             set listOfFilesOnHost to listOfFilesOnHost & (tcp read stream nlst_stream)
  107.         end repeat
  108.         
  109.         tcp close stream nlst_stream
  110.         
  111.         set oldDelimiters to AppleScript's text item delimiters
  112.         set AppleScript's text item delimiters to {ASCII character 13}
  113.         set listOfFiles to text items of listOfFilesOnHost
  114.         set AppleScript's text item delimiters to ""
  115.         set listOfFilesOnHost to listOfFiles as string
  116.         set AppleScript's text item delimiters to {ASCII character 10}
  117.         set listOfFiles to text items of listOfFilesOnHost
  118.         set AppleScript's text item delimiters to oldDelimiters
  119.         
  120.         if ftp_transfer_type ≠ 2 then
  121.             tcp write stream sss data "TYPE I" & return using ISO88591
  122.             readresponse(sss)
  123.         else
  124.             tcp write stream sss data "TYPE A" & return using ISO88591
  125.             readresponse(sss)
  126.         end if
  127.         
  128.         repeat with indexFile in listOfFiles
  129.             set indexFile to contents of indexFile
  130.             if indexFile = "" then
  131.                 exit repeat
  132.             end if
  133.             
  134.             set data_stream to (tcp wait for connect)
  135.             set data_stream_status to (tcp status stream data_stream)
  136.             set portHiByte to round (local port of data_stream_status) / 256 rounding down
  137.             set portLoByte to (local port of data_stream_status) mod 256
  138.             
  139.             tcp write stream sss data "PORT " & localAddrCommaString & "," & portHiByte & "," & portLoByte & return using ISO88591
  140.             readresponse(sss)
  141.             
  142.             tcp write stream sss data "RETR " & indexFile & return & LF -- using ISO88591
  143.             readresponse(sss)
  144.             
  145.             repeat until (connection status of (tcp status stream data_stream) = Connected)
  146.             end repeat
  147.             
  148.             try
  149.                 set indexFullFileName to ftp_localname & indexFile
  150.                 if ftp_transfer_type = 1 then
  151.                     tcp receive stream data_stream destination file indexFullFileName using MacBinary maximum seconds 10
  152.                 else if ftp_transfer_type = 2 then
  153.                     tcp receive stream data_stream destination file indexFullFileName using ISO88591 maximum seconds 10
  154.                 else if ftp_transfer_type = 3 then
  155.                     tcp receive stream data_stream destination file indexFullFileName using Raw Data Fork maximum seconds 10
  156.                 end if
  157.             on error msg number num from obj partial result pr
  158.                 tcp close stream data_stream
  159.                 error msg number num from data_stream
  160.             end try
  161.             
  162.             tcp close stream data_stream
  163.             readresponse(sss)
  164.             
  165.         end repeat
  166.         
  167.         tcp write stream sss data "QUIT" & return using ISO88591
  168.         readresponse(sss)
  169.         
  170.         tcp close stream sss
  171.         return
  172.     on error msg number num from obj partial result pr
  173.         try
  174.             tcp write stream sss data "QUIT" & return using ISO88591
  175.             readresponse(sss)
  176.         on error
  177.         end try
  178.         
  179.         tcp close stream sss
  180.         error msg number num from obj partial result pr
  181.     end try
  182. end run
  183.  
  184. on readresponse(sstream)
  185.     set continuechar to ""
  186.     set wholemessage to ""
  187.     repeat until continuechar = " "
  188.         repeat until (tcp ahead characters LF stream sstream)
  189.         end repeat
  190.         set readline to (tcp read stream sstream until characters LF using ISO88591)
  191.         set scan to (scanline(readline))
  192.         set continuechar to item 2 of scan
  193.         set wholemessage to wholemessage & " " & item 3 of scan
  194.     end repeat
  195.     set errorCode to item 1 of scan as integer
  196.     if (errorCode ≥ 400) then
  197.         display dialog "Error: " & wholemessage
  198.         error wholemessage number errorCode
  199.     end if
  200. end readresponse
  201.  
  202. -- Matches lines like:
  203. -- 250 test... Sender ok
  204.  
  205. on scanline(lline)
  206.     return {characters 1 through 3 of lline as string, character 4 of lline as string, ¬
  207.         characters 5 through end of lline as string}
  208. end scanline